1 00:00:00,450 --> 00:00:01,290 Welcome back. 2 00:00:01,320 --> 00:00:04,320 In this lecture we're going to be learning about functions. 3 00:00:04,350 --> 00:00:10,110 Functions are powerful tools that allow us to execute the same code over and over again for multiple 4 00:00:10,110 --> 00:00:11,430 places in our scripts. 5 00:00:11,460 --> 00:00:16,350 A function itself is a block of code that accomplishes a specific task. 6 00:00:16,470 --> 00:00:22,530 A function can be called from places within our scripts to perform the task it's designed to complete. 7 00:00:22,560 --> 00:00:27,180 To call a function means we would like the function to execute the code inside of it. 8 00:00:27,230 --> 00:00:31,950 A function can also have information passed to it, and the function can also return information back 9 00:00:31,950 --> 00:00:33,000 to where it was called. 10 00:00:33,030 --> 00:00:35,550 You'll be using functions all of the time in your scripts. 11 00:00:35,550 --> 00:00:37,920 So let's go ahead and see how to create one. 12 00:00:38,160 --> 00:00:42,880 So the first thing we need to do to create a function is type out the function keyword. 13 00:00:42,900 --> 00:00:48,240 After we type out the function keyword, we need to create an identifier or name for this function. 14 00:00:48,240 --> 00:00:50,340 That way it's easy for us to call it. 15 00:00:50,370 --> 00:00:54,610 This is the exact same thing for creating an identifier for a variable. 16 00:00:54,630 --> 00:00:58,140 So I'm going to call this function here get some. 17 00:00:58,140 --> 00:01:02,400 And after we create the identifier we need to create a set of parentheses. 18 00:01:02,400 --> 00:01:08,140 And inside of these parentheses is where we can define information that can be passed to this function. 19 00:01:08,160 --> 00:01:11,280 This function in particular I want to have numbers passed to it. 20 00:01:11,280 --> 00:01:16,800 And I want this function to calculate the sum of all those numbers and return that sum back to where 21 00:01:16,800 --> 00:01:17,920 this function was called. 22 00:01:17,940 --> 00:01:23,340 So for example, for now I can create something called a parameter inside of this function. 23 00:01:23,370 --> 00:01:26,430 This first parameter I'm going to call number one. 24 00:01:26,430 --> 00:01:29,340 And then to create another parameter I just put a comma. 25 00:01:29,340 --> 00:01:32,460 And then I'm going to create a second parameter called number two. 26 00:01:32,820 --> 00:01:37,080 These are called parameters because these are just acting as placeholders right. 27 00:01:37,110 --> 00:01:41,460 These are placeholders for the actual values that are going to be passed to the function. 28 00:01:41,490 --> 00:01:45,090 The actual values passed to the function are known as the arguments. 29 00:01:45,120 --> 00:01:49,470 While these are known as the placeholder values or the parameters. 30 00:01:49,980 --> 00:01:52,560 Afterwards, we create a new block of code. 31 00:01:52,560 --> 00:01:53,580 Here after hitting enter. 32 00:01:53,580 --> 00:01:58,500 As you can see by this end statement, and inside of here is where we can create all of the code that 33 00:01:58,500 --> 00:02:00,720 this particular function is supposed to complete. 34 00:02:00,720 --> 00:02:06,120 For this function, I would like to add number one and number two together, and then return that result 35 00:02:06,120 --> 00:02:07,750 back to where this function is called. 36 00:02:07,770 --> 00:02:09,600 So that's going to be really easy to do. 37 00:02:09,630 --> 00:02:11,460 We can create a variable in here. 38 00:02:11,550 --> 00:02:13,770 I'm going to call this variable sum. 39 00:02:13,770 --> 00:02:20,640 And it's going to be equal to the number one passed to this function plus the number two passed to this 40 00:02:20,640 --> 00:02:21,270 function. 41 00:02:21,270 --> 00:02:26,310 And then once we get this sum we can return that value back to where this function was called by using 42 00:02:26,310 --> 00:02:27,480 another keyword. 43 00:02:27,480 --> 00:02:29,760 And that is the return keyword. 44 00:02:29,760 --> 00:02:34,860 And this keyword allows us to exit out of this function and go back to where it was called. 45 00:02:34,860 --> 00:02:38,760 And with that we can pass a value we would like to return as well. 46 00:02:38,940 --> 00:02:43,860 And you can also return multiple values by placing a comma and typing out the other values that you 47 00:02:43,860 --> 00:02:46,920 would like to return back to where this function was called. 48 00:02:47,010 --> 00:02:48,060 Pretty simple. 49 00:02:48,360 --> 00:02:52,980 So to call this function, we type out the name for it or the identifier. 50 00:02:52,980 --> 00:02:55,140 In this case it is get some. 51 00:02:55,140 --> 00:02:58,560 And then afterwards we have to pass our two parentheses. 52 00:02:58,560 --> 00:03:03,240 And here we can see that we can pass two different parameters to our function number one and number 53 00:03:03,270 --> 00:03:03,870 two. 54 00:03:03,960 --> 00:03:07,140 So for example I'm going to pass the number five. 55 00:03:07,140 --> 00:03:08,970 And then I can pass the number ten. 56 00:03:09,390 --> 00:03:14,310 And this is us calling the function or basically telling the interpreter to have the main thread of 57 00:03:14,310 --> 00:03:16,440 execution hop into this function. 58 00:03:16,440 --> 00:03:20,420 It's going to take these values with it and it's going to go into our function. 59 00:03:20,430 --> 00:03:26,280 Five is placed inside of this parameter number one and ten is placed in the second parameter number 60 00:03:26,280 --> 00:03:26,860 two. 61 00:03:26,880 --> 00:03:32,100 It executes this code here which is adding number one and number two together storing it in this variable. 62 00:03:32,100 --> 00:03:37,410 And then it encounters the return keyword along with the value that we've just calculated. 63 00:03:37,410 --> 00:03:42,990 And it hops back to where this function was called and returns it, which means we can store the return 64 00:03:43,020 --> 00:03:44,910 of this function inside of a variable. 65 00:03:44,910 --> 00:03:46,650 So I'm going to create another variable here. 66 00:03:46,650 --> 00:03:48,150 I'm going to call it result. 67 00:03:48,180 --> 00:03:52,320 And it's going to be equal to the return of this function. 68 00:03:52,840 --> 00:03:56,890 And then we can go ahead and print this result inside of our console. 69 00:03:57,010 --> 00:04:00,190 So if we go to our test tab and then hit run. 70 00:04:01,820 --> 00:04:05,300 As you can see, we get our result of 15, which is ten plus five. 71 00:04:05,330 --> 00:04:06,200 Very cool. 72 00:04:07,280 --> 00:04:11,510 The awesome thing about functions is you can call them as many times as you'd like. 73 00:04:11,540 --> 00:04:17,480 So after we calculate the sum here, maybe we want to calculate another sum so we can override the result 74 00:04:17,480 --> 00:04:19,070 variable equal to get some. 75 00:04:19,070 --> 00:04:22,250 And this time I'll pass some numbers like 20 and 30. 76 00:04:22,250 --> 00:04:25,070 And then we can print the result back into the console. 77 00:04:25,190 --> 00:04:31,370 We could also override result again and get the sum of maybe -20 and 500. 78 00:04:31,370 --> 00:04:33,270 And then print that result out. 79 00:04:33,290 --> 00:04:38,390 So there's a lot of awesome ways we can use functions and call them wherever inside of our scripts. 80 00:04:38,390 --> 00:04:44,450 Instead of us having to copy and paste this code in multiple places, all we need to do is call this 81 00:04:44,450 --> 00:04:51,890 singular function that is, storing the code responsible for calculating the sum of two numbers. 82 00:04:51,890 --> 00:04:59,330 So if we go ahead and hit run, as you can see, we get our totals in the console 1550 and 480. 83 00:04:59,360 --> 00:05:00,110 Awesome. 84 00:05:00,230 --> 00:05:06,050 Now, just like variables, functions can also be created in a global or local scope. 85 00:05:06,050 --> 00:05:11,090 So right here our function has been created in the global scope of the script. 86 00:05:11,090 --> 00:05:16,010 If I would like to restrict this function to the local scope, then all I need to do is type the local 87 00:05:16,010 --> 00:05:17,510 keyword before my function. 88 00:05:17,510 --> 00:05:21,590 And now I have created it in the local scope. 89 00:05:21,590 --> 00:05:29,420 Or basically it is only accessible after the function has been declared, because when I make my function 90 00:05:29,420 --> 00:05:35,150 global, I'm actually able to access get some even before it gets created. 91 00:05:35,150 --> 00:05:37,640 And this is only in special circumstances. 92 00:05:37,670 --> 00:05:42,260 As an example, if I had another function in here, I'm just going to call this example. 93 00:05:42,380 --> 00:05:50,240 Inside of this function I can actually use git sum even though git sum is created after this function 94 00:05:50,240 --> 00:05:50,330 is. 95 00:05:50,360 --> 00:05:54,260 And that's because git sum is in the global scope of the script. 96 00:05:54,290 --> 00:06:00,920 However, if I restrict it using the local keyword, all of a sudden we have no idea what git sum is. 97 00:06:00,950 --> 00:06:05,960 And that's because git sum is now only accessible after it has been created. 98 00:06:05,960 --> 00:06:10,940 So that's a very important difference to note when you make a function global, and when you restrict 99 00:06:10,940 --> 00:06:16,280 that function to a scope, most of the time you'll want to have your functions in a local scope, but 100 00:06:16,280 --> 00:06:21,680 there will be instances where you might want to create a function globally within your scripts. 101 00:06:22,820 --> 00:06:29,600 Now, another cool thing that we can do with functions is actually define a variable or variadic number 102 00:06:29,600 --> 00:06:32,160 of parameters to be passed to the function. 103 00:06:32,180 --> 00:06:33,410 What do I mean by this? 104 00:06:33,440 --> 00:06:36,860 Well, let's say we want to get the sum of numbers right. 105 00:06:36,860 --> 00:06:40,670 But we want to be able to pass as many numbers as we want to this function. 106 00:06:40,670 --> 00:06:43,940 We don't want to be restricted to only two parameters. 107 00:06:43,970 --> 00:06:49,220 Well, in order to support this, what we can do is we can replace the parameters up top here in our 108 00:06:49,220 --> 00:06:51,980 function with three dots. 109 00:06:51,980 --> 00:06:58,970 And these three dots now represent a variable number of arguments that can be passed to this function. 110 00:06:58,970 --> 00:07:02,900 We have no idea how many numbers might be passed to this function. 111 00:07:02,900 --> 00:07:09,410 So that's why we're using the three dots to represent a variadic number of arguments or parameters. 112 00:07:09,590 --> 00:07:13,250 Now what we can do is we can create a table here. 113 00:07:13,250 --> 00:07:16,220 And I'm just going to call this all numbers. 114 00:07:16,810 --> 00:07:22,600 And we can store all of the numbers passed to this function by typing out the three dots and storing 115 00:07:22,600 --> 00:07:24,310 it inside of our table. 116 00:07:24,430 --> 00:07:30,310 So if we pass like let's say 30 different numbers to this function, they're now all going to be stored 117 00:07:30,310 --> 00:07:32,200 inside of this table or this array. 118 00:07:32,380 --> 00:07:34,270 And what's the great thing about arrays. 119 00:07:34,270 --> 00:07:37,390 Well they're organized and we can loop through them. 120 00:07:37,390 --> 00:07:42,490 And since we can loop through arrays that means we can grab every single number in the array, add them 121 00:07:42,490 --> 00:07:44,820 up altogether and then return that sum. 122 00:07:44,830 --> 00:07:53,200 So what we're going to do is we're going to loop through every single value inside of the all numbers 123 00:07:53,200 --> 00:07:53,710 array. 124 00:07:53,710 --> 00:08:00,460 So we use the Ipairs function and then pass out all numbers and then put the do keyword. 125 00:08:00,460 --> 00:08:04,560 And now we get every single value or number inside of our tables. 126 00:08:04,570 --> 00:08:07,630 You might recognize that I have named this with an underline. 127 00:08:07,630 --> 00:08:11,410 And this simply means that I do not care about this variable here. 128 00:08:11,410 --> 00:08:15,010 I can name it whatever I like, like I can name it hello high whatever. 129 00:08:15,010 --> 00:08:15,910 Or I could keep it at. 130 00:08:15,910 --> 00:08:16,420 I. 131 00:08:16,420 --> 00:08:22,030 But since in this particular instance where I'm looping through this array, I do not care for the index. 132 00:08:22,030 --> 00:08:23,320 I only want the values. 133 00:08:23,320 --> 00:08:28,180 So I denote it with an underline to signify that I do not care about this value here. 134 00:08:28,180 --> 00:08:31,180 This is just kind of my throw away variable. 135 00:08:31,720 --> 00:08:36,640 But once we do this, I'm also going to create another variable and I'm going to call it sum. 136 00:08:36,880 --> 00:08:38,860 We'll set it equal to zero. 137 00:08:38,950 --> 00:08:45,460 And then inside of my for loop what I want to do is I want to set the value of sum equal to itself plus 138 00:08:45,460 --> 00:08:49,340 this value we're grabbing inside of our array. 139 00:08:49,360 --> 00:08:54,010 And another cool thing with Lua is there's actually a faster way to type this out. 140 00:08:54,010 --> 00:09:01,540 Instead of saying sum is equal to sum plus v, we can use the plus equal operator which achieves the 141 00:09:01,540 --> 00:09:02,830 exact same effect. 142 00:09:02,830 --> 00:09:07,280 So we're taking sum and we're adding v to it plus equal. 143 00:09:07,300 --> 00:09:08,260 Very nice. 144 00:09:08,290 --> 00:09:13,240 After we have calculated the sum for all of the numbers that gets passed to this function, then we 145 00:09:13,240 --> 00:09:15,590 can return it back to where this function was called. 146 00:09:15,610 --> 00:09:21,910 So this time, instead of being restricted to only passing two numbers, I can now pass as many numbers 147 00:09:21,910 --> 00:09:22,380 as I like. 148 00:09:22,390 --> 00:09:29,140 I can pass 30, I could pass a thousand, I could pass -50, and so on 1.29. 149 00:09:29,140 --> 00:09:35,470 And I could just keep passing as many numbers as I like, because they're all going to be stored inside 150 00:09:35,470 --> 00:09:36,310 of this table. 151 00:09:36,310 --> 00:09:41,590 And we're going to loop through that table and add up all the numbers together and return that back 152 00:09:41,590 --> 00:09:43,120 to where this function was called. 153 00:09:43,150 --> 00:09:44,920 So now if we hit run. 154 00:09:46,100 --> 00:09:51,710 As you can see, I get my very nice total of 1,126.29. 155 00:09:51,740 --> 00:09:55,000 It took all of those numbers, placed it inside of the table. 156 00:09:55,010 --> 00:09:59,840 We looped through that table, added up all the numbers together, and then returned it back to where 157 00:09:59,840 --> 00:10:01,100 that function was called. 158 00:10:01,710 --> 00:10:04,050 This is the very nice power of functions. 159 00:10:04,050 --> 00:10:07,480 We don't have to copy all this code in multiple different places. 160 00:10:07,500 --> 00:10:09,690 It's only in one area of our script. 161 00:10:09,690 --> 00:10:16,350 And to execute all of this code, all we need to do is use the identifier, call the function and pass 162 00:10:16,350 --> 00:10:18,810 whatever data we would like to our function. 163 00:10:18,810 --> 00:10:19,860 It's awesome. 164 00:10:20,190 --> 00:10:25,950 Now another thing I would like to show you with functions are something called recursive functions. 165 00:10:25,980 --> 00:10:32,010 A recursive function is a function that calls itself either directly or indirectly. 166 00:10:32,040 --> 00:10:36,600 This can cause an infinite loop of the function calling itself over and over again. 167 00:10:36,600 --> 00:10:41,730 Unless you provide an opportunity to break out of this loop by a condition that needs to be evaluated. 168 00:10:41,730 --> 00:10:47,790 So basically, this is another fancy way of creating an infinite loop by having a function call itself 169 00:10:47,790 --> 00:10:49,250 over and over and over again. 170 00:10:49,260 --> 00:10:52,380 So to demonstrate this I'm going to create a new function. 171 00:10:52,650 --> 00:10:56,400 And I'm just going to call this function recursion example. 172 00:10:57,500 --> 00:10:58,340 And inside. 173 00:10:58,340 --> 00:11:04,030 At the very end of this function, what I want to do is call recursion example again. 174 00:11:04,040 --> 00:11:09,680 So the first time I call this function within my script, it is going to execute this function over 175 00:11:09,680 --> 00:11:12,680 and over and over and over again for infinity. 176 00:11:12,710 --> 00:11:17,710 Unless I provide an opportunity to break out of this recursive loop. 177 00:11:17,720 --> 00:11:21,410 So to do that I'm going to create a variable just before my function. 178 00:11:21,410 --> 00:11:24,290 I'm just going to call it I and set it equal to zero. 179 00:11:24,440 --> 00:11:30,980 And each time this function gets executed, I want to update the value of I plus equal one. 180 00:11:31,130 --> 00:11:36,680 And then we also need to add a yield in this function if we want it to execute forever. 181 00:11:36,680 --> 00:11:42,530 Because just like a while loop, if we have a recursive function that executes for infinity and it does 182 00:11:42,530 --> 00:11:45,350 not yield, then we're going to crash our game. 183 00:11:45,380 --> 00:11:51,380 However, since I want this recursion example function to execute for a finite number of times, I do 184 00:11:51,380 --> 00:11:55,760 not need to add a yield statement, but I'm just going to do it for this example. 185 00:11:55,760 --> 00:11:58,640 So we're going to wait for one second in here. 186 00:11:59,600 --> 00:12:04,370 And what I'm going to do, or the first thing I want to do in this function is I want to check if I 187 00:12:04,400 --> 00:12:06,220 is greater than ten. 188 00:12:06,230 --> 00:12:12,740 So if I becomes greater than ten, then we can use the return keyword to go ahead and exit out of this 189 00:12:12,740 --> 00:12:13,310 function. 190 00:12:13,310 --> 00:12:18,740 We can use the return keyword to prematurely break out of this function, even if we don't have any 191 00:12:18,740 --> 00:12:19,850 values to return. 192 00:12:19,850 --> 00:12:25,190 If we ever store any result from this function, it's simply going to store it as nil. 193 00:12:25,220 --> 00:12:27,590 Because again, we're not returning any values here. 194 00:12:27,590 --> 00:12:30,740 This just allows us to break out of this function early. 195 00:12:31,280 --> 00:12:36,260 So each time through this loop, we're going to be adding one waiting one second calling itself again. 196 00:12:36,260 --> 00:12:39,600 And it's going to keep checking until I becomes greater than ten. 197 00:12:39,620 --> 00:12:43,580 Once it does, the loop breaks and the recursive function is broken. 198 00:12:44,120 --> 00:12:47,300 So what I'm also going to do is print out I into the console. 199 00:12:48,390 --> 00:12:54,270 And then we can go ahead and call our function, because this function isn't going to execute until 200 00:12:54,270 --> 00:12:59,940 we called it simply, we are just instantiating the function here and we're instantiating the function 201 00:12:59,940 --> 00:13:00,300 here. 202 00:13:00,300 --> 00:13:04,530 But the code inside of it will not execute until we actually call it. 203 00:13:04,530 --> 00:13:10,890 By using the identifiers and passing the parentheses and any other possible values we'd like to add 204 00:13:10,890 --> 00:13:12,870 or send to this function. 205 00:13:13,720 --> 00:13:17,550 So now we can hit run and inside of the console. 206 00:13:17,560 --> 00:13:22,780 As you can see, every single second it is counting up and adding one to our I variable. 207 00:13:22,780 --> 00:13:26,560 And eventually, once it goes beyond ten, there we go. 208 00:13:26,560 --> 00:13:27,580 It has stopped. 209 00:13:27,580 --> 00:13:34,990 It hit 11, realized that 11 is greater than ten, and now that loop or that recursive function loop 210 00:13:34,990 --> 00:13:35,980 has broken. 211 00:13:36,780 --> 00:13:42,840 Recursion is just another tool we have in programming that you likely won't use often, but it's good 212 00:13:42,840 --> 00:13:48,030 to know that it's available for you if you ever need to use recursive functions. 213 00:13:48,540 --> 00:13:49,080 All righty. 214 00:13:49,080 --> 00:13:51,550 And that's really all there is to functions. 215 00:13:51,570 --> 00:13:56,430 They are simple blocks of code that can take an input and give us an output. 216 00:13:56,500 --> 00:14:00,000 Hope you're learning a lot so far and I'll see you in the next lecture.